﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using SharpBits.Base;
using WinServiceBase;
using WinServiceBase.Utils;

namespace WinServiceBase.WinServices
{
    [RunInstaller(true)]
    public partial class AutoDownloadsServiceInstaller : Installer
    {
        public AutoDownloadsServiceInstaller()
        {
            ServiceProcessInstaller procInstller = new ServiceProcessInstaller();
            procInstller.Account = ServiceAccount.NetworkService;

            ServiceInstaller srvInstller = new ServiceInstaller()
            {
                ServiceName = AutoDownloadsServiceHost.InstallServiceName,
                Description = "Service to automatically create BITS download jobs from url files.",
                StartType = ServiceStartMode.Automatic
            };

            Installers.Add(procInstller);
            Installers.Add(srvInstller);
        }
    }

    public class AutoDownloadsServiceHost : AutoDownloadsService
    {
        public const string InstallServiceName = "AutoDownloadsService";

        public static string BaseDir
        {
            get { return AppDomain.CurrentDomain.BaseDirectory; }
        }
        public static string GetLogsFile()
        {
            string result = ConfigurationManager.AppSettings["logs-file"] + "";
            if(!Directory.Exists(Path.GetDirectoryName(result)))
                result = Path.Combine(BaseDir, Path.GetFileName(result));

            return result;
        }
        public static void Main(string[] args)
        {
            if(InstallOrUninstall(args, new AutoDownloadsServiceInstaller()))
                return;

            AutoDownloadsService service = new AutoDownloadsService()
            {
                DisplayName = "Auto Downloads Service",
                ServiceName = InstallServiceName
            };

            var inputRegex = ConfigurationManager.AppSettings["downloads-input-files-regex"];
            var inputDir = Path.IsPathRooted(ConfigurationManager.AppSettings["downloads-input-dir"])
                                    ? ConfigurationManager.AppSettings["downloads-input-dir"]
                                    : Path.Combine(BaseDir, ConfigurationManager.AppSettings["downloads-input-dir"]);
            var outputDir = Path.IsPathRooted(ConfigurationManager.AppSettings["downloads-output-dir"])
                                    ? ConfigurationManager.AppSettings["downloads-output-dir"]
                                    : Path.Combine(BaseDir, ConfigurationManager.AppSettings["downloads-output-dir"]);
            var downloadsThread = new DownloadsThread(inputRegex, GetLogsFile(), inputDir, outputDir)
            {
#if DEBUG
                Interval = 300
#else
                Interval = string.IsNullOrEmpty(ConfigurationManager.AppSettings["check-interval"])
                           ? 15 * 60 * 1000
                           : (int)(float.Parse(ConfigurationManager.AppSettings["check-interval"]) * 60 * 1000)
#endif
            };
            service.WorkerThreads = new WorkerThread[] { downloadsThread };
            ServiceBase[] servicesToRun = new ServiceBase[] { service };
            Run(servicesToRun, args);
        }
    }

    public class AutoDownloadsService : WindowsService
    {
        public AutoDownloadsService()
        {
        }
    }
}
